import { createVNode, render } from "vue";
import PlaySvga from "./PlaySvga.vue";

function getContainer() {
  const container = document.getElementById("svga-container");
  if (container) {
    return container;
  }
  const divContainer = document.createElement("div");
  divContainer.id = "svga-container";
  // 添加到body上
  document.body.appendChild(divContainer);
  return divContainer;
}
// 创建svga动画
export function createSvga(options) {
  const { src, loops, mode, onlyTag, onFrame, onFinished } = options;
  const container = getContainer();
  const div = document.createElement("div");
  function destroy() {
    render(null, div);
    div.remove();
  }
  // 添加到body上
  container.appendChild(div);
  const vNode = createVNode(PlaySvga, {
    src,
    loops,
    mode,
    destroy,
    onlyTag,
    onFrame,
    onFinished
  });
  render(vNode, div);
}
